# Contributor: Cameron <cbanta@gmail.com>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=pgpool
_pkgname="$pkgname-II"
pkgver=4.1.2
pkgrel=0
pkgdesc="A connection pooling/replication server for PostgreSQL"
url="https://www.pgpool.net/"
arch="all"
license="pgpool-II"
makedepends="postgresql-dev linux-headers"
pkgusers="postgres"
pkggroups="postgres"
install="$pkgname.pre-install $pkgname.post-upgrade"
subpackages="$pkgname-openrc $pkgname-doc $pkgname-static $pkgname-dev"
source="$pkgname-$pkgver.tar.gz::https://www.pgpool.net/download.php?f=$_pkgname-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotated
	musl-compat.patch
	fix-listen_address.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"

prepare() {
	default_prepare
	update_config_sub
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc/$pkgname \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-openssl
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	touch "$pkgdir"/etc/$pkgname/pool_passwd

	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -m644 -D "$srcdir"/$pkgname.logrotated "$pkgdir"/etc/logrotate.d/$pkgname

	install -d -o "$pkgusers" -g "$pkggroups" -m755 "$pkgdir"/var/log/$pkgname
}

doc() {
	local docdir="$subpkgdir"/usr/share/doc/$_pkgname
	default_doc

	mkdir -p "$docdir"
	mv "$pkgdir"/etc/$pkgname/* "$docdir"/

	local f; for f in pcp.conf pgpool.conf pool_hba.conf; do
		cp "$docdir"/$f.sample "$pkgdir"/etc/$pkgname/$f
	done
}

sha512sums="979cf03e1c6d407e056e94dcc2efd90a99514672742677f733348ec673252b13b8e3a5487160783e7c8619dea8b9a8f6a739f222013d9c2f6d1ab0c36912a65e  pgpool-4.1.2.tar.gz
71b8239b1b29e2c4a8312b300122ced1452bbe60fc7937e80172c7c5e3d6be71e5aee58f6d3d687b0e35df6ccdc27125a12ae9098f7c2d07e76b8103abca3556  pgpool.initd
0e40a681b068ce5c7f03c342c1217b170601a507cacdf120b9a308df65f2065e6085b292a393802d1955079f7ec434a412e6d871f688ad83bc33fa34aca37cfe  pgpool.confd
c9aa2ea9484ed29cb57cdff4004fa9dd4780d73c69db3378effb2e0ecd3ae178771c6a847a28e1a9cc6492ada4321584afb92c9b592119fb11898b42191f22b1  pgpool.logrotated
37e8314f2dab6889c35edb679906db3997c4d5eba704a7337ff82926d400f2ab780103b6a162b1effa74c0d7f8d6655b62cddd2017d3ea7a5de5f370871ab088  musl-compat.patch
625447bd0e5ef7687bcff2d020ddbdf788eec05abeb4f7979f078c15e6969fada8569fe233a543f133a939dac1fae4eb6513d7bbd7333f6f976c9d583ba5857a  fix-listen_address.patch"
